package io.finer.erp.jeecg.jolimark.print;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.finer.erp.jeecg.jolimark.comm.Config;
import io.finer.erp.jeecg.jolimark.comm.HttpClientUtil;
import io.finer.erp.jeecg.jolimark.model.AccessTokenRequestModel;
import org.apache.commons.lang.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 创建访问令牌
 * 
 * @author Jolimark
 *
 */
public class CreateAccessToken {

	/**
	 * 获取访问令牌
	 */
	public void accessToken() {
		AccessTokenRequestModel model = new AccessTokenRequestModel(Config.getAppID(), Config.getAppKey());
		String paramString = model.buildRequestParameter();

		String requestURLString = Config.getUrlV2() + "GetAccessToken?" + paramString;
		String reponseString = HttpClientUtil.sendHttpGet(requestURLString);
		JSONObject responseJson = JSON.parseObject(reponseString);
		if(responseJson.getInteger("return_code").equals(0)){
			String returnDataStr = responseJson.getString("return_data");
			if(StringUtils.isNotBlank(returnDataStr)){
				JSONObject returnData = JSON.parseObject(returnDataStr);
				Config.setAccessToken(returnData.getString("access_token"));
			}
		}
	}

}
